package br.com.cardoso.videostore.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.cardoso.web.filter.ApplicationFilter;

import br.com.cardoso.videostore.application.VideoStoreConstants;
import br.com.cardoso.videostore.model.entity.UserEntity;

@WebFilter(value = "/pages/*")
public class ApplicationSecurityFilter extends ApplicationFilter implements Filter {

	@Override
	public void destroy() {
	
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		
		UserEntity userLogged = (UserEntity) getUserLogged(servletRequest);
		
		if (userLoggedIsNull(userLogged)) {
			redirect(servletResponse, getApplicationContext(servletRequest) + VideoStoreConstants.PAGE_LOGIN);
		} else if (accessGranted(userLogged, servletRequest)){
			filterChain.doFilter(servletRequest, servletResponse);			
		} else {
			
		}

	}

	private boolean accessGranted(UserEntity userLogged, ServletRequest servletRequest) {
		return true;
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	
	}

}